using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Audio;

using ZombieHigh2.Controller;
using ZombieHigh2.Interfaces;
using ZombieHigh2.Audio;
using ZombieHigh2.Inventory;
using ZombieHigh2.World;
using QuadAnimation.Animation;

namespace ZombieHigh2.Actor
{
    public class HumanPlayer : BaseActor
    {
        const float footStepDelay = 0.4f;
        float footStepTime = 0f;

        PlayerInventory inventory;

        public PlayerInventory Inventory
        {
            get { return inventory; }
        }

        public HumanPlayer(ActionEvents actions, AudioSystem audio, AnimatedObject animatedObject)
            : base(actions, audio, animatedObject)
        {
            InputController = new Human();
            inventory = new PlayerInventory();
        }

        public override void Load()
        {
            base.Load();
            //Quad = new AnimatedQuad(Vector3.Zero, Vector3.Backward, Vector3.Up, 96, 128, 384, 512, 96, 128);
            //Quad.SetFrame(0);
        }

        public override void Update(GameTime gameTime)
        {
            base.Update(gameTime);

            float elapsedTime = (float)gameTime.ElapsedGameTime.TotalSeconds;

            ControllerData inputData = InputController.GetControllerData();
            inputData.LAnalog *= 16f;

            Velocity.X += inputData.LAnalog.X;
            Velocity.Z -= inputData.LAnalog.Y;


            if (inputData.LAnalog.X < 0)
            {
                Direction = PlayerDirection.PDIR_LEFT;
            }
            else if (inputData.LAnalog.X > 0)
            {
                Direction = PlayerDirection.PDIR_RIGHT;
            }

            if (inputData.XPressed)
            {
                SetCurrentState(PlayerState.PSTATE_ATTACKING);
                //Attack = true;
            }
            else if (CurrentPlayerState != PlayerState.PSTATE_ATTACKING)
            {
                if ((Velocity.X > 0) || (Velocity.X < 0))
                {
                    if (footStepTime > footStepDelay)
                    {
                        Audio.PlayCue("foot_step");
                        footStepTime = 0f;
                    }

                    SetCurrentState(PlayerState.PSTATE_WALKING);
                }
                else if (LastPlayerState == PlayerState.PSTATE_WALKING)
                {
                    SetCurrentState(PlayerState.PSTATE_IDLE);
                }
            }

            //if (inputData.APressed)
            //{
            //    Velocity.Y += 16.0f;
            //}

            footStepTime += elapsedTime;

            
        }

        protected override void OnAnimationEnd()
        {
            //Could use the current state as well
            //if (CurrentPlayerState == PlayerState.PSTATE_ATTACKING)
            //{
            //    Actions.Player_DoMeleeAttack(this);
            //    SetAnimationCycle("Idle");
            //    CurrentPlayerState = PlayerState.PSTATE_IDLE;
            //}
            if (CurrentPlayerState == PlayerState.PSTATE_ATTACKING)
            {
                Actions.Player_DoMeleeAttack(this);
                SetCurrentState(PlayerState.PSTATE_IDLE);
            }
        }

        public override void Info_Damage(BaseActor who, int amount)
        {
            Health -= amount;

            if (amount < 0)
                IsDead = true;
        }

        public void TouchItem(int itemType)
        {
            switch (itemType)
            {
                case WorldConstants.WEAPON_CAN:
                    inventory.AddSprayCan();
                    break;

                case WorldConstants.WEAPON_KNIFE:
                    inventory.AddKnife();
                    break;
            }
        }
    }
}
